package com.xinyan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.fasterxml.jackson.databind.util.BeanUtil;
import com.xinyan.config.RedisKeyConstant;
import com.xinyan.dto.SprayHostDTO;
import com.xinyan.pojo.SprayHost;
import com.xinyan.mapper.SprayHostMapper;
import com.xinyan.service.ISprayHostService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 雾森主机表 服务实现类
 * </p>
 *
 * @author xzlf
 * @since 2024-03-21 23:29:03
 */
@Service
@Slf4j
public class SprayHostServiceImpl extends ServiceImpl<SprayHostMapper, SprayHost> implements ISprayHostService {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private SprayHostMapper sprayHostMapper;

    @Value("${redis.cache.hostinfo.time}")
    private int timeout;

    @Override
    public boolean addOne(SprayHost sprayHost) {
        sprayHost.setStatus(1);
        updateRedisHost(sprayHost.getPhone());
        return save(sprayHost);
    }


    @Override
    public List<SprayHost> findAllHost(String phone) {
        Object obj = redisTemplate.opsForValue().get(RedisKeyConstant.PRE_USER_HOST + phone);
        if (Objects.nonNull(obj)) {
            log.info("======直接从缓存获取了数据，关联手机号为：{}======", phone);
            return (List<SprayHost>) obj;
        }
        List<SprayHost> list = updateRedisHost(phone);
        return list;
    }

    @Nullable
    private List<SprayHost> updateRedisHost(String phone) {
        List<SprayHost> list = list(new LambdaQueryWrapper<SprayHost>().eq(SprayHost::getPhone, phone));
        if (!CollectionUtils.isEmpty(list)) {
            redisTemplate.opsForValue().set(RedisKeyConstant.PRE_USER_HOST + phone, list);
            redisTemplate.expire(RedisKeyConstant.PRE_USER_HOST + phone, timeout, TimeUnit.MINUTES);
            log.info("======更新了缓存，关联手机号为：{}======", phone);
        }
        return list;
    }

    @Override
    public boolean addHost(List<SprayHost> hostList) {
        boolean b = saveBatch(hostList);
        updateRedisHost(hostList.get(0).getPhone());
        return b;
    }

    @Override
    public boolean updateHost(SprayHost sprayHost) {
        boolean b = updateById(sprayHost);
        updateRedisHost(sprayHost.getPhone());
        return b;
    }

    @Override
    public PageDTO<SprayHostDTO> findByPage(long current, long size, String phone) {
        PageDTO<SprayHost> sprayHostPageDTO = sprayHostMapper.selectPage(new PageDTO<SprayHost>(current, size),
                new LambdaQueryWrapper<SprayHost>().eq(SprayHost::getPhone, phone));
        PageDTO<SprayHostDTO> dtoPageDTO = new PageDTO<>();


        List<SprayHost> records = sprayHostPageDTO.getRecords();
        List<SprayHostDTO> dtoRecords = new ArrayList<>();
        records.forEach(item -> {
            SprayHostDTO dto = new SprayHostDTO();
            BeanUtils.copyProperties(item,dto);
            dtoRecords.add(dto);
        });
        dtoPageDTO.setRecords(dtoRecords);
        dtoPageDTO.setTotal(sprayHostPageDTO.getTotal());
        dtoPageDTO.setSize(sprayHostPageDTO.getSize());
        dtoPageDTO.setCurrent(sprayHostPageDTO.getCurrent());

        return dtoPageDTO;
    }
}
